package dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import beans.Branch;
import beans.Company;

import utils.HibernateUtil;






public class CompanyDao {
	private SessionFactory sessionFactory;

	public CompanyDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	Transaction tr = null;
	
	/**
	 * 删除公司
	 */
	public void delete(Company company) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(company);
		tr.commit();
		session.close();
	}
	
	/**
	 * 修改公司
	 */
	public void edit(Company company) {

		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(company);
		tr.commit();
		session.close();
	}
	
	/**
	 * 保存新增的公司
	 */
	public void save(Company company) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(company);
		tr.commit();
		session.close();
	}
	
	/**
	 * 返回所有公司信息
	 */
	public List<Company> getAllcompany(){
		Session session=sessionFactory.openSession();
		tr=session.beginTransaction();
		Criteria c=session.createCriteria(Company.class);
		List<Company> companys=c.list();
		tr.commit();
		session.close();
		return companys;
	}
	
	/**
	 * 
	 * 获取分页
	 * @author Devil Zheng
	 * @created 2015-9-16 下午3:06:18
	 * @return
	 */
	public List<Company> getCompanyes(int rows, int page){
	    Session session=sessionFactory.openSession();
	    tr=session.beginTransaction();
	    Criteria c=session.createCriteria(Company.class);
	    c.setFirstResult((page-1)*rows);
	    c.setMaxResults(rows);
	    List<Company> companys=c.list();
	    tr.commit();
	    session.close();
	    return companys;
	}

	/**
	 * 通过公司ID返回指定公司信息
	 * 
	 * @return
	 */
	public Company getCompanyById(int companyId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Company company = (Company) session.get(Company.class, companyId);
		tr.commit();
		session.close();
		return company;
	}
}
